﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace hlavny.modul2
{
    [Serializable]
    public class CSysDatabases
    {
        private List<CDatabase> databases = null;
        private int lastId = 0;

        [NonSerialized]
        private CDatabase currentDatabase = null;

        #region CSysDatabases properties

        public List<CDatabase> Databases
        {
            get { return databases; }
            set { databases = value; }
        }
        public int LastId
        {
            get { return lastId; }
            set { lastId = value; }
        }
        public CDatabase CurrentDatabase
        {
            get { return currentDatabase; }
            set { currentDatabase = value; }
        }

        #endregion

        #region CSysDatabases constroctor overload

        public CSysDatabases()
        {
            databases = new List<CDatabase>();
        }

        #endregion

        #region CSysDatabases help functions

        /// <summary>
        /// Fuction search for database id and return index of database in sysDatabases
        /// </summary>
        /// <param name="id">id of the database</param>
        /// <returns>index of database in sysDatabases, if not found returns negative number</returns>
        private int GetDatabaseIndex(int id)
        {
            return databases.BinarySearch(new CDatabase(id));
        }

        /// <summary>
        /// Function find out if the database with entered name already exists
        /// </summary>
        /// <param name="name">name of the database</param>
        /// <returns>true if database already exists, otherwise false</returns>
        public bool ContainDatabaseName(string name)
        {
            if (databases == null) return false;
            foreach (CDatabase db in databases)
            {
                if (db.Name == name) return true;
            }
            return false;
        }

        #endregion

        #region CSysDatabases main functions

        /// <summary>
        /// Function create db with entered name
        /// </summary>
        /// <param name="name">name of the db</param>
        /// <returns>CDatabase object</returns>
        public CDatabase CreateDB(string name)
        {
            this.lastId++;
            CDatabase db = new CDatabase(this.lastId, name);
            databases.Add(db);
            databases.Sort();
            Save();
            return db;
        }

        /// <summary>
        /// Add table for the current database
        /// </summary>
        /// <param name="table">table to add</param>
        /// <returns>true if adding done, otherwise false</returns>
        public bool AddTable(CTable table)
        {
            if (currentDatabase != null)
            {
                currentDatabase.TablesIds.Add(table.Id);
                Save();
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Function drop table from the current database
        /// </summary>
        /// <param name="table">table to drop</param>
        /// <returns>true if table dropped, otherwise false</returns>
        public bool DropTable(CTable table)
        {
            int index = currentDatabase.TablesIds.BinarySearch(table.Id);
            if (index < 0)
            {
                return false;
            }
            else
            {
                currentDatabase.TablesIds.RemoveAt(index);
                currentDatabase.TablesIds.Sort();
                Save();
                return true;
            }
        }

        /// <summary>
        /// Function drop entered database
        /// </summary>
        /// <param name="database">database to drop</param>
        /// <returns>true if the database dropped, otherwise false</returns>
        public bool DropDatabase(CDatabase database)
        {
            int index = GetDatabaseIndex(database.Id);
            if (index < 0)
            {
                return false;
            }
            else
            {
                databases.RemoveAt(index);
                databases.Sort();
                Save();
            }
            return true;
        }
        
        #endregion

        #region Save function

        public void Save()
        {
            CFunctions.StoreSysDatabases(this);
        }
        
        #endregion
    }
}
